<template>
  <div class="index-page">
    <h3>店铺信息编辑</h3>
    <div class="row" style="padding:20px;">
      <input type="button" class="btn btn-info" @click="addNewOPtion()" value="新增店铺信息">
    </div>
    <div class="row_height row">
      <div class="col-md-4" v-for="(item,index) in main_list" :key="index">
        <div class="img_box">
          <img :src="item.img_url" alt="" class="full-width">
        </div>
        <h4>{{ item.title }}</h4>
        <p>{{ item.content }}</p>
        <div class="btn_box">
          <input type="button" value="删除" @click="deleteData(item)" class="btn btn-danger">
        </div>
      </div>
    </div>
    <!--新增弹窗代码-->
    <el-dialog title="新增店铺" :visible.sync="dialog_show">
      <el-form enctype="multipart/form-data" id="uploadForm">
        <el-form-item label="选择图片" label-width="120px">
          <input type="file" name="store_img" id="add_img">
        </el-form-item>
        <el-form-item label="店铺名" label-width="120px">
          <el-input auto-complete="off" type="textarea" v-model="form.store_name" name="store_name"></el-input>
        </el-form-item>
        <el-form-item label="店铺信息" label-width="120px">
          <el-input auto-complete="off" type="textarea" v-model="form.store_infor" name="store_infor"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_show = false">取 消</el-button>
        <el-button type="primary" @click="add_new()">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
import g from '../../components/global.js'

export default {
  components: {},
  data() {
    return {
      form: {
        store_name: '',
        store_infor: ''
      },
      dialog_show: false,
      pic_url: '',
      main_list: [],
      changeDialog: {
        pic_url: '',
        dialog_show: false,
        res_id: ''
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    //新增 弹窗显示
    addNewOPtion() {
      clear()
      this.dialog_show = true
    },
    //新增 提交数据
    add_new() {
      var that = this
      var formData = new FormData($('#uploadForm')[0])
      $.ajax({
        url: g.res_url + '/api/v1/shop_information',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data == 'success') {
            that.$message({
              message: '操作成功！',
              type: 'success'
            })
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络繁忙！')
        }
      })
      that.dialog_show = false
    },
    ts_dialog(cont) {
      this.$alert(cont, '提示', {
        confirmButtonText: '确定'
      })
    },
    //获取数据
    getData() {
      $.ajax({
        url: g.res_url + '/api/v1/shop_getData',
        type: 'post',
        success: data => {
          this.main_list = JSON.parse(data).data

        }
      })
    },
   
    deleteData(row) {
      var that = this
      $.ajax({
        url: g.res_url + '/api/v1/shopinfor_del',
        type: 'post',
        data: {
          shop_id: row.id
        },
        success: function(data) {
          if (data == 'success') {
            that.$message.success('操作成功！')
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(err) {
          that.$message.error('网络错误！')
        }
      })
    }
  }
}
function clear() {
  $('#change_img').val('')
  $('#add_img').val('')
}
</script>
<style scoped>
.row_height {
  height: 150px !important;
  margin: 28px 0;
}
.btn_box {
  margin-left: 45px;
}
.img_box {
  width: 200px;
  height: 200px;
  overflow: hidden;
  margin-bottom: 20px;
}
.full-width {
  width: 100%;
}
.col-md-4 {
  margin-top: 30px;
}
</style>